package com.ys.controller.api.finance;

import com.ys.config.MenuId;
import com.ys.entity.finance.TcwFinanceSalaryClassify;
import com.ys.entity.sys.YsCol;
import com.ys.service.finance.TcwFinanceSalaryClassifyService;
import com.ys.service.sys.CommonApiService;
import com.ys.service.sys.UploadService;
import com.ys.util.ExportExcelUtils;
import com.ys.util.admin.ConditionUtil;
import com.ys.vo.LayuiTableVoJA;
import com.ys.vo.R;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

/**
 * 财务工资小计部门维护表
 */
@Controller
@RequestMapping("/api/inter/tcwFinanceSalaryClassify")
public class ApiInterTcwFinanceSalaryClassifyController {
    private static final Logger logger = LoggerFactory.getLogger(ApiInterTcwFinanceSalaryClassifyController.class);

    @Autowired
    private CommonApiService<TcwFinanceSalaryClassify> commonApiService;

    @Autowired
    private TcwFinanceSalaryClassifyService tcwFinanceSalaryClassifyService;



    /**
     * 设置查询的cond的sql设置
     * @param cond
     * @return
     */
    public TcwFinanceSalaryClassify setSeachSql(TcwFinanceSalaryClassify cond){

        cond.addLeftJoin("TS_OrganizationInfo O ON O.Orgid = A.Orgid ")
                //.addField("O.OrgName AS orgName")//部门
        .addField("CASE " +
                "WHEN O.Orgid = 'D250' THEN '研发部' " +
                "WHEN O.Orgid = 'C101' THEN '总裁办' " +
                " ELSE O.OrgName END orgName")//部门名称

        //排序
        .setOrderby("A.ClassifyName ASC,A.Orgid ASC");

        cond = commonApiService.dataAuth(MenuId.TCW_FinanceSalaryClassify, cond);
        return  cond;
    }


    /**
     * 查询
     */
    @RequiresPermissions("tcwFinanceSalaryClassify:view")
    @RequestMapping("/search")
    @ResponseBody
    public LayuiTableVoJA search(Model model, HttpServletRequest request, HttpServletResponse response,
                                 @ModelAttribute TcwFinanceSalaryClassify cond,String conditions
    ){
        try {
            cond = setSeachSql(cond);
            LayuiTableVoJA setList = commonApiService.searchLay(cond);

            return setList;
        }catch (Exception e){
            logger.error("查询出错：",e.getMessage(),e);
            LayuiTableVoJA tableVoJA = new LayuiTableVoJA();
            tableVoJA.setCode(1);
            tableVoJA.setMsg("查询出错，请稍后重试！");
            return tableVoJA;
        }
    }

    /**
     * 新增
     */
    @RequestMapping("/toAdd")
    @ResponseBody
    public R toSave(HttpServletRequest request, @RequestParam Map<String, Object> params){
        try {
            //新增
            return tcwFinanceSalaryClassifyService.add(params);
        } catch (Exception e) {
            logger.error("新增出错：",e.getMessage(),e);
            return R.error(e.getCause().getMessage());
        }
    }


    /**
     * 修改
     */
    @RequestMapping("/toEdit")
    @ResponseBody
    public R toEdit(HttpServletRequest request, @RequestParam Map<String, Object> params){
        try {
            //修改
            return tcwFinanceSalaryClassifyService.update(params);
        } catch (Exception e) {
            logger.error("修改出错：",e.getMessage(),e);
            return R.error(e.getCause().getMessage());
        }
    }


    /**
     * 删除
     */
    @PostMapping("/toDel")
    @ResponseBody
    public R toDel(Long id){
        try {
            return tcwFinanceSalaryClassifyService.del(id);
        } catch (Exception e) {
            e.printStackTrace();
            return R.error(e.getCause().getMessage());
        }
    }

    /**
     * 删除列表
     */
    @RequestMapping("/toDelByItem")
    @ResponseBody
    public R toDelByItem(HttpServletRequest request,@RequestParam("list") List<String> idList){
        try {
            //删除
            for (int i = 0; i < idList.size(); i++) {
                tcwFinanceSalaryClassifyService.del(Long.parseLong(idList.get(i)));
            }
            return R.ok();
        } catch (Exception e) {
            logger.error("删除出错：",e.getMessage(),e);
            return R.error("系统正在开小差，请联系客服，我们将第一时间为您解决！");
        }
    }




}